<?php 

class Flickr
{
	function Flickr()
	{
		$this->obj =& get_instance();
		$this->get_settings();
		$this->fields = array (
			array 
			(
				'label' => __("Flickr api key", "album"),
				'input' => 'flickr_api_key',
				'info' => anchor("http://www.flickr.com/services/api/keys/", __("Get it here", "album"), array('target' => '_blank')),
				'default' => ''
			),
			array 
			(
				'label' => __("Flickr api secret", "album"),
				'input' => 'flickr_api_secret',
				'info' => anchor("http://www.flickr.com/services/api/keys/", __("Get it here", "album"), array('target' => '_blank')),
				'default' => '' 
			),
			array 
			(
				'label' => __("Flickr api token", "album"),
				'input' => 'flickr_api_token',
				'info' => anchor("http://phpflickr.com/tools/auth/", __("Get it here", "album"), array('target' => '_blank')),
				'default' => '' 
			),
			array 
			(
				'label' => __("Flickr screen name", "album"),
				'input' => 'flickr_api_username',
				'info' => anchor("http://www.flickr.com/account/prefs/screenname/", __("Get it here", "album"), array('target' => '_blank')),
				'default' => '' 
			)
			
		);
	}
	
	function store($image_data)
	{
		
		$this->obj->load->plugin('phpflickr');	
		$this->f = new phpFlickr($this->album_settings->flickr_api_key, $this->album_settings->flickr_api_secret);
		$this->f->setToken($this->album_settings->flickr_api_token);
		$this->f->enableCache("fs", "./cache");
		
		if($photo_id = $this->f->sync_upload($image_data['full_path']))
		{
			
			if ($photo = $this->f->photos_getInfo($photo_id))
			{
				$result = array(
				's' => $this->f->buildPhotoURL($photo, "Square"),
				't' => $this->f->buildPhotoURL($photo, "Thumbnail"),
				'm' => $this->f->buildPhotoURL($photo, "Small"),
				'o' => $this->f->buildPhotoURL($photo, "Medium")
				);
				@unlink($image_data['full_path']);
				return $result;
			}
			else
			{
				
				return false;
			}
		}
		else
		{
			
			return false;
		}

	}

	function get_settings()
	{
		$query = $this->obj->db->get('album_settings');
		if ($query->num_rows() > 0)
		{
		   foreach ($query->result() as $row)
		   {
			  $this->album_settings->{$row->name} = $row->value;
		   }
		}			
	}

}
		
